package com.wolfalpha.utron.service.impl;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Log;

import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemInformation;
import com.wolfalpha.utron.bean.ItemProject;
import com.wolfalpha.utron.bean.UserBaseInfo;
import com.wolfalpha.utron.bean.UserInfo;
import com.wolfalpha.utron.exception.LoginException;
import com.wolfalpha.utron.net.URLs;
import com.wolfalpha.utron.service.UserService;
import com.wolfalpha.utron.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by luodon on 15/8/17.
 */
public class UserServiceImpl implements UserService {
    private String TAG = "UserServiceImpl";

    @Override
    public UserInfo login(String acc, String pwd) throws LoginException {
        Map<String, String> mp = new HashMap<String, String>();
        mp.put("account", acc);
        mp.put("password", pwd);
        String result = AppContext.getApiClient().sendPostResquest(URLs.LOGIN_VALIDATE_HTTP, mp);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result == null) {
            throw new LoginException("网络错误");
        }
        if (JsonUtils.getRet(result) != 200) {
            throw new LoginException(JsonUtils.getMsg(result));
        }
        return result == null ? null : JsonUtils.paraseJson(JsonUtils.getData(result), UserInfo.class);
    }

    @Override
    public void anonLogin(String mobile_model, String mobile_os, int apk_code) {
        String url = URLs.ANONLOGIN_VALIDATE_HTTP;
        System.out.println(url);
        Map<String, String> mp = new HashMap<String, String>();
        mp.put("mobile_model", mobile_model);
        mp.put("mobile_os", mobile_os);
        mp.put("apk_code", String.valueOf(apk_code));
        String result = AppContext.getApiClient().sendPostResquest(url, mp);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean register(String phone, String nickname, String pwd, String scode) {
        Map<String, String> mp = new HashMap<String, String>();
        mp.put("phone", phone);
        mp.put("nickname", nickname);
        mp.put("password", pwd);
        mp.put("school_code", scode);

        String result = AppContext.getApiClient().sendPostResquest(URLs.USER_REGISTER, mp);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result == null ? false : JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean getPhoneCode(final String phone) {
        String url = URLs.PHONE_GET_CODE + phone;
        System.out.println(url);
        String result = AppContext.getApiClient().sendGetResquest(url);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result == null ? false : JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean checkPhoneCode(final String phone, final String code) {
        Map<String, String> mp = new HashMap<String, String>();
        mp.put("phone", phone);
        mp.put("code", code);


        String result = AppContext.getApiClient().sendPostResquest(URLs.PHONE_CHECK_CODE, mp);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonUtils.getRet(result) == 200;

//        /**
//         * TODO 此处方便测试，关闭验证码通道
//         */
//        return true;
    }

    @Override
    public boolean checkNickIsUnique(final String nickname) {
        String url = URLs.USER_CHECK_NICKNAME + nickname;
        String result = AppContext.getApiClient().sendGetResquest(url);
        return result == null ? false : JsonUtils.getRet(result) == 200;
    }

    @Override
    public boolean updateIndentity(UserInfo userInfo) {
        Map<String, String> mp = new HashMap<String, String>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
//        System.out.println("---------------------------" + AppContext.getInstance().getUserId());
        mp.put("nickname", userInfo.getNickname());
        mp.put("sex", String.valueOf(userInfo.getSex()));
        mp.put("sign", userInfo.getUtron_sign());
        mp.put("mail", userInfo.getMail());
        mp.put("intro", userInfo.getIntroduction());
        mp.put("home", userInfo.getHome());
        mp.put("age", String.valueOf(userInfo.getAge()));
        mp.put("school", String.valueOf(userInfo.getSchool()));
        String result = AppContext.getApiClient().sendPostResquest(URLs.USER_URL + "updateIdentity", mp);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result == null ? false : JsonUtils.getRet(result) == 200;
    }

    @Override
    public UserInfo getUserInfo(int id) {
        UserInfo userinfo;//= new UserInfo();
        Map<String, String> mp = new HashMap<String, String>();
        mp.put("user_id", String.valueOf(id));
        String result = AppContext.getApiClient().sendPostResquest(URLs.USER_URL + "getIdentity", mp);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result == null || JsonUtils.getRet(result) != 200) {
            return null;
        } else {
            userinfo = JsonUtils.paraseJson(JsonUtils.getData(result), UserInfo.class);
            return userinfo;
        }
    }


    @Override
    public String updateUserHead(File pic_url) {
        Map<String, String> mp = new HashMap<String, String>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        Map<String, File> fmp = new HashMap<String, File>();
        fmp.put("upfile", pic_url);
        String result = null;
        try {
            result = AppContext.getApiClient().uploadFile(URLs.USER_UPDATE_HEAD, mp, fmp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return JsonUtils.getData(result);
    }

    @Override
    public Bitmap getUserHead(String url) {
        return AppContext.getApiClient().getusericon(url);
    }

    @Override
    public List<UserBaseInfo> getMyPartnerAttentions(int page) {
        List<UserBaseInfo> baseInfos = new ArrayList<UserBaseInfo>();
        Map<String, String> mp = new HashMap<>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("page", String.valueOf(page));
        String result = AppContext.getApiClient().sendPostResquest(URLs.USER_URL + "getAttentionPartners", mp);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result == null || TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                UserBaseInfo userBaseInfo = JsonUtils.paraseJson(arr.get(i).toString(), UserBaseInfo.class);
                baseInfos.add(userBaseInfo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

        return baseInfos;
    }

    @Override
    public List<ItemProject> getMyProjectAttentions(int page) {
        List<ItemProject> projects = new ArrayList<ItemProject>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        String result = AppContext.getApiClient().sendPostResquest(URLs.USER_URL + "getAttentionProjects", mp);
        try {
            Log.i(TAG, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result == null || TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }

        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemProject project = JsonUtils.paraseJson(arr.get(i).toString(), ItemProject.class);
                projects.add(project);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
        return projects;
    }

    @Override
    public List<ItemInformation> getMyCollections(int page) {
        List<ItemInformation> itemInformations = new ArrayList<ItemInformation>();
        Map<String, String> mp = new HashMap<>();
        mp.put("page", String.valueOf(page));
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        String result = AppContext.getApiClient().sendPostResquest(URLs.USER_URL + "getMyCollections", mp);
        if (result == null) {
            return null;
        }
        System.out.println(result);
        if (TextUtils.isEmpty(JsonUtils.getData(result))) {
            return null;
        }
        JSONArray arr = null;
        try {
            arr = new JSONArray(JsonUtils.getData(result));
            for (int i = 0; i < arr.length(); i++) {
                ItemInformation information = JsonUtils.paraseJson(arr.get(i).toString(), ItemInformation.class);
                itemInformations.add(information);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return itemInformations;
    }

    @Override
    public boolean attention(int userId, boolean status) {
        Map<String, String> mp = new HashMap<String, String>();
        mp.put("user_id", String.valueOf(AppContext.getInstance().getUserId()));
        mp.put("to_id", String.valueOf(userId));
        mp.put("operation", status ? "add" : "cancel");
        String result = null;
        try {
            result = AppContext.getApiClient().sendPostResquest(URLs.USER_URL + "attentionUser", mp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (result == null) {
            return false;
        }
        System.out.println(result);
        return JsonUtils.getRet(result) == 200;
    }
}
